package com.example.demo.controller;

import com.example.demo.bean.HttpResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/image")
public class ImageController {
    @RequestMapping("tofileupload")
    public String tofileupload(){
        return "fileupload";
    }
    @RequestMapping("upload")
    @ResponseBody
    public HttpResult<Boolean> upload(MultipartFile file) throws IOException {
        HttpResult<Boolean> result = new HttpResult<>();
        if(file.isEmpty()){
            result.setMsg("文件无效");
            result.setData(false);
        }else{
            String filename = file.getOriginalFilename();
            filename = System.currentTimeMillis()+filename;
            file.transferTo(new File("D:\\upload/"+filename));
            result.setMsg("上传成功");
            result.setData(true);
        }
        return result;
    }
}

